<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="800px"
    class="dialogClass"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="135px"
    >
      <el-form-item label="年份" prop="trendYear">
        <el-date-picker
          clearable
          v-model="formData.trendYear"
          type="year"
          value-format="yyyy"
          placeholder="请选择年份"
          :maxlength="50"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="立案数(件)" prop="filCase">
        <el-input
          v-model.trim="formData.filCase"
          placeholder="请输入立案数"
          maxlength="9"
          clearable
          type="number"
          oninput="value=value.replace(/[^0-9]/g,'')"
        />
      </el-form-item>
      <el-form-item label="涉案总金额(元)" prop="caseAmount">
        <el-input
          v-model.trim="formData.caseAmount"
          placeholder="请输入涉案总金额"
          :maxlength="15"
          clearable
          oninput="value=value.replace(/[^0-9.]/g,'')"
        />
      </el-form-item>
      <el-form-item label="涉案人数(人)" prop="casePeoples">
        <el-input
          v-model.trim="formData.casePeoples"
          placeholder="请输入涉案人数"
          maxlength="9"
          clearable
          type="number"
          oninput="value=value.replace(/[^0-9]/g,'')"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">取消</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  listTrend,
  getTrend,
  delTrend,
  addTrend,
  updateTrend,
} from "@/api/caseManage/caseStage/trend";

import {
  MODALTYPE,
  CASESTATE,
  PAGESIZE,
  PAGENUM,
  DEPTTYPE,
  ROLETYPE,
  CASEEXPORTURL,
} from "@/utils/variable";

import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  inheritAttrs: false,
  dicts: ["case_stage", "comple_status"],
  props: {
    modalType: {
      type: String,
      require: true,
    },
    id: {
      type: Number,
    },
  },

  data() {
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      temCaseName: "",
      // 是否显示弹出层
      open: false,
      formData: {
        trendYear: "",
        filCase: "",
        caseAmount: "",
        casePeoples: "",
      },
      rules: {
        trendYear: [
          { required: true, message: "年份不能为空", trigger: "blur" },
        ],
        filCase: [
          { required: true, message: "立案数不能为空", trigger: "blur" },
        ],
        caseAmount: [
          { required: true, message: "涉案总金额不能为空", trigger: "blur" },
        ],
        casePeoples: [
          { required: true, message: "涉案人数不能为空", trigger: "blur" },
        ],
      },
    };
  },

  computed: {
    resetButtonContent() {
      if (this.modalType == MODALTYPE["详情"]) {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },

  watch: {
    modalType: {
      handler(value) {
        if (value == MODALTYPE["新增"]) {
          this.reset();
          this.open = true;
          this.title = "新增案件趋势";
        } else if (value == MODALTYPE["修改"]) {
          this.reset();
          if (this.id) {
            getTrend(this.id).then((response) => {
              this.formData = response.data;
              this.temCaseName = this.formData.caseName;
              this.open = true;
              this.title = "修改案件趋势";
            });
          }
        } else if (value == "detail") {
          this.isDisabled = true;
          this.reset();
          if (this.id) {
            getTrend(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "案件趋势详情";
            });
          }
        }
      },
      immediate: true,
    },
  },

  methods: {
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", "");
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        trendYear: "",
        filCase: "",
        caseAmount: "",
        casePeoples: "",
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == MODALTYPE["修改"]) {
            updateTrend(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == MODALTYPE["新增"]) {
            addTrend(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/dialog.scss"; //模态框样式

::v-deep.treeselect {
  font-size: 12px !important;
  font-family: $font-family !important;
  font-weight: $form-label-font-weight !important;
}
</style>
